//
//  NSVipPopView.m
//  Meboo
//
//  Created by kyo on 2024/5/21.
//

#import "NSVipPopView.h"
#import <TYCyclePagerView/TYCyclePagerView.h>
#import "NSVipPopPageView.h"
#import "NSVipBuyPopView.h"
#import "NSVipPopBannerCell.h"
#import "NSVipItemView.h"
#import "NSVipFuncItemView.h"
#import "KLPaymentManager.h"
#import "NSProductModel.h"

@interface NSVipPopView ()
<
TYCyclePagerViewDelegate,
TYCyclePagerViewDataSource
>
@property (nonatomic, strong) UIImageView *backImageView;
//顶部轮播图
@property (nonatomic , strong) TYCyclePagerView *cycleScrollView;
//分页视图
@property (nonatomic, strong) NSVipPopPageView *pageView;
@property (nonatomic, strong) UIButton *closeBtn;
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIButton *continueBtn;
@property (nonatomic, strong) NSMutableArray <NSVipItemView *>*viewArr;
@property (nonatomic, strong) UIImageView *funcsHeaderImageView;
@property (nonatomic, strong) NSMutableArray <MBVipEquityModel *>*dataArr;
//fc购买成功
@property (nonatomic, strong) NSVipBuyPopView *buyPopView;
//product
@property (nonatomic, copy) NSString *productID;
@end

@implementation NSVipPopView

#pragma mark - Life Cycle
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
        [[MBMineDataManager sharedManager] getMemberProductWithType:11 success:^(BOOL success) {
            if (success) {
                self.dataArr = [NSMutableArray arrayWithArray:[MBMineDataManager sharedManager].vipEquityArr];
                //根据图片张数 设置pageView
                [self.pageView mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.width.mas_equalTo(MBWidthScale(12)*self.dataArr.count-MBWidthScale(6));
                }];
                [self.cycleScrollView reloadData];
                self.pageView.totalNum = self.dataArr.count;
                self.pageView.index = 0;
                //根据场景 确定定位弹窗
                if (self.index < self.dataArr.count) {
                    [self.cycleScrollView scrollToItemAtIndex:self.index animate:NO];
                    self.pageView.index = self.index;
                }
                                
                if ([MBMineDataManager sharedManager].vipProductArr) {
                    [self setProductUI];
                    [self setFuncsView];
                }
            }
        }];
    }
    return self;
}

#pragma mark - Private Method
- (void)setProductUI {
    __WeakSelf
    for (int i = 0; i < 3; i++) {
        NSVipItemView *view = self.viewArr[i];
        view.tag = i;
        NSProductModel *model = [MBMineDataManager sharedManager].vipProductArr[i];
        if (i == 1) {
            model.isSelect = YES;
            self.productID = model.productCode;
        } else {
            model.isSelect = NO;
        }
        view.model = model;
        view.clickBlock = ^(NSInteger tag) {
            [weakSelf clickItemAction:tag];
            weakSelf.productID = model.productCode;
        };
        [self.viewArr replaceObjectAtIndex:i withObject:view];
    }
}

- (void)clickItemAction:(NSInteger)tag {
    for (int i = 0; i<self.viewArr.count; i++) {
        NSVipItemView *view = self.viewArr[i];
        NSProductModel *model = view.model;
        if (i == tag) {
            model.isSelect = YES;
            self.productID = model.productCode;
        } else {
            model.isSelect = NO;
        }
        view.model = model;
        [self.viewArr replaceObjectAtIndex:i withObject:view];
    }
}

- (void)setFuncsView {
    for (int i = 0; i < self.dataArr.count; i++) {
        NSVipFuncItemView *view = [NSVipFuncItemView new];
        view.model = self.dataArr[i];
        [self.scrollView addSubview:view];
        [view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.scrollView);
            make.width.mas_equalTo(ScreenWidth-MBWidthScale(40));
            make.height.mas_equalTo(MBHeightScale(70));
            make.top.equalTo(self.funcsHeaderImageView.mas_bottom).offset(MBHeightScale(20)+MBHeightScale(80*i));
            if (i == self.dataArr.count-1) {
                make.bottom.mas_equalTo(MBHeightScale(-66));
            }
        }];
    }
}

- (void)setIndex:(NSInteger)index {
    _index = index;
    //根据场景 确定定位弹窗
    if (index < self.dataArr.count) {
        [self.cycleScrollView scrollToItemAtIndex:index animate:NO];
        self.pageView.index = index;
    }
}

#pragma mark - Action
- (void)closeAction {
    self.hidden = YES;
}

- (void)nextAction {
    __WeakSelf
    self.backImageView.userInteractionEnabled = NO;
    [[KLPaymentManager sharePaymentManager] payWithProductID:self.productID];
    [KLPaymentManager sharePaymentManager].payBlock = ^(BOOL payState) {
        self.backImageView.userInteractionEnabled = YES;
        if (payState) {
            weakSelf.hidden = YES;
            weakSelf.buyPopView.hidden = NO;
            if (weakSelf.payBlock) {
                weakSelf.payBlock();
            }
        }
    };
}

#pragma mark - TYCyclePagerViewDataSource
- (NSInteger)numberOfItemsInPagerView:(TYCyclePagerView *)pageView {
    return self.dataArr.count;
}

- (UICollectionViewCell *)pagerView:(TYCyclePagerView *)pagerView cellForItemAtIndex:(NSInteger)index {
    
    NSVipPopBannerCell *cell = [pagerView dequeueReusableCellWithReuseIdentifier:@"NSVipPopBannerCell" forIndex:index];
    cell.model = self.dataArr[index];
    return cell;
}

- (TYCyclePagerViewLayout *)layoutForPagerView:(TYCyclePagerView *)pageView {
    TYCyclePagerViewLayout *layout = [[TYCyclePagerViewLayout alloc]init];
    layout.itemSize = CGSizeMake(CGRectGetWidth(pageView.frame), CGRectGetHeight(pageView.frame));
    layout.itemSpacing = 0;
    return layout;
}

- (void)pagerView:(TYCyclePagerView *)pageView didScrollFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex {
    self.pageView.index = toIndex;
}

#pragma mark -- TYCyclePagerViewDelegate
- (void)pagerView:(TYCyclePagerView *)pageView didSelectedItemCell:(__kindof UICollectionViewCell *)cell atIndex:(NSInteger)index {
    [pageView scrollToNearlyIndexAtDirection:TYPagerScrollDirectionRight animate:YES];
}

#pragma mark - UI
- (void)setupUI {
    self.hidden = YES;
    self.backgroundColor = [UIColor colorWithRed:0 / 255.0 green:0 / 255.0 blue:0 / 255.0 alpha:0.5];
    
    [self addSubview:self.backImageView];
    [self.backImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(MBWidthScale(20));
        make.height.mas_equalTo((ScreenWidth-MBHeightScale(40))/336*495);
        make.center.equalTo(self);
    }];
    
    [self.backImageView addSubview:self.scrollView];
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.backImageView);
    }];
    
    [self.scrollView addSubview:self.cycleScrollView];
    [self.cycleScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(self.scrollView);
        make.width.mas_equalTo(ScreenWidth-MBWidthScale(40));
        make.height.mas_equalTo(MBHeightScale(165));
    }];
    
    [self.scrollView addSubview:self.pageView];
    [self.pageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(MBWidthScale(6));
        make.top.equalTo(self.cycleScrollView.mas_bottom).offset(MBHeightScale(20));
        make.centerX.equalTo(self.cycleScrollView);
    }];
    
    //三个支付类目
    CGFloat wid = (ScreenWidth-MBWidthScale(84))/3;
    for (int i = 0; i < 3; i++) {
        NSVipItemView *view = [NSVipItemView new];
        view.index = i;
        [self.scrollView addSubview:view];
        [view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(MBWidthScale(14) + (MBWidthScale(8)+wid)*i);
            make.width.mas_equalTo(wid);
            make.height.mas_equalTo(MBHeightScale(133));
            make.top.equalTo(self.pageView.mas_bottom).offset(MBHeightScale(25));
        }];
        [self.viewArr addObject:view];
    }
    
    [self.scrollView addSubview:self.funcsHeaderImageView];
    [self.funcsHeaderImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.pageView.mas_bottom).offset(MBHeightScale(178));
        make.left.mas_equalTo(MBWidthScale(11));
        make.width.mas_equalTo(ScreenWidth-MBWidthScale(60));
        make.height.mas_equalTo((ScreenWidth-MBWidthScale(60))/314*21);
    }];
    
    [self.backImageView addSubview:self.continueBtn];
    [self.continueBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(MBWidthScale(11));
        make.right.mas_equalTo(MBWidthScale(-11));
        make.height.mas_equalTo(MBHeightScale(48));
        make.bottom.mas_equalTo(MBHeightScale(-16));
    }];
    
    [self.backImageView addSubview:self.closeBtn];
    [self.closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(MBWidthScale(-25));
        make.width.height.mas_equalTo(MBHeightScale(18));
        make.top.mas_equalTo(MBHeightScale(15));
    }];
    
    [kKeyWindow addSubview:self.buyPopView];
}

#pragma mark - Setter && Getter

- (UIImageView *)backImageView {
    if (!_backImageView) {
        _backImageView = [UIImageView new];
        _backImageView.userInteractionEnabled = YES;
        _backImageView.image = ImageWithName(@"wlm_vip_pop_back");
    }
    return _backImageView;
}

- (NSMutableArray<NSVipItemView *> *)viewArr {
    if (!_viewArr) {
        _viewArr = [NSMutableArray array];
    }
    return _viewArr;
}

- (UIImageView *)funcsHeaderImageView {
    if (!_funcsHeaderImageView) {
        _funcsHeaderImageView = [UIImageView new];
        _funcsHeaderImageView.image = [UIImage imageNamed:@"mine_vipfucs_header_image"];
    }
    return _funcsHeaderImageView;
}

- (UIButton *)continueBtn {
    if (!_continueBtn) {
        _continueBtn = [UIButton new];
        _continueBtn.layer.cornerRadius = MBHeightScale(48/2);
        [_continueBtn setTitle:@"Purchase Now" forState:0];
        [_continueBtn setTitleColor:HEXS(@"ffffff") forState:0];
        _continueBtn.titleLabel.font = MBHelBoldFontSizeScale(16);
        [_continueBtn setBackgroundImage:ImageWithName(@"wlm_vip_purchase_back") forState:0];
        [_continueBtn addTarget:self action:@selector(nextAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _continueBtn;
}

- (UIButton *)closeBtn {
    if (!_closeBtn) {
        _closeBtn = [UIButton new];
        [_closeBtn setYXEnlargeEdgeWithTop:10 right:10 bottom:10 left:10];
        [_closeBtn setBackgroundImage:ImageWithName(@"wlm_vip_pop_close") forState:0];
        [_closeBtn addTarget:self action:@selector(closeAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _closeBtn;
}

- (TYCyclePagerView *)cycleScrollView {
    if (!_cycleScrollView) {
        _cycleScrollView = [[TYCyclePagerView alloc] init];
        _cycleScrollView.isInfiniteLoop = YES;
        _cycleScrollView.delegate = self;
        _cycleScrollView.dataSource = self;
        _cycleScrollView.backgroundColor = [UIColor clearColor];
        _cycleScrollView.autoScrollInterval = 3;
        _cycleScrollView.reloadDataNeedResetIndex = YES;
        [_cycleScrollView registerClass:[NSVipPopBannerCell class] forCellWithReuseIdentifier:@"NSVipPopBannerCell"];
    }
    return _cycleScrollView;
}

- (NSVipPopPageView *)pageView {
    if (!_pageView) {
        _pageView = [NSVipPopPageView new];
    }
    return _pageView;
}

- (UIScrollView *)scrollView {
    if (!_scrollView) {
        _scrollView = [UIScrollView new];
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.backgroundColor = UIColor.clearColor;
    }
    return _scrollView;
}

- (NSVipBuyPopView *)buyPopView {
    if (!_buyPopView) {
        _buyPopView = [[NSVipBuyPopView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
    }
    return _buyPopView;
}

- (NSMutableArray<MBVipEquityModel *> *)dataArr {
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}
@end
